package com.yaozheng.vocationaltraining.fragment;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.TextView;

import com.yaozheng.vocationaltraining.R;
import com.yaozheng.vocationaltraining.adapter.CarTypeDetailListAdapter;
import com.yaozheng.vocationaltraining.bean.CarDetailBean;
import com.yaozheng.vocationaltraining.bean.DownPaymentBean;
import com.yaozheng.vocationaltraining.bean.RepaymentReriodBean;
import com.yaozheng.vocationaltraining.utils.LogUtils;
import com.yaozheng.vocationaltraining.utils.TypeUtils;
import com.yaozheng.vocationaltraining.view.PagerSlidingTabStrip;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.ViewById;

import java.util.ArrayList;
import java.util.List;

import static android.R.attr.fragment;

/**
 * Created by moon on 2017/11/21.
 */

@EFragment(R.layout.activity_price)
public class PriceFragment extends BaseFragment {
    @ViewById
    TextView headTitleText;

    @ViewById
    PagerSlidingTabStrip tabs;

    @ViewById
    ViewPager pager;

    private String[] titles = { "全款", "贷款" };

    private AllPriceFragment allPriceFragment; //全款
    private LoanPriceFragment loanPriceFragment;//贷款

    public CarDetailBean getCarDetailBean() {
        return carDetailBean;
    }

    public void setCarDetailBean(CarDetailBean carDetailBean) {
        this.carDetailBean = carDetailBean;
    }

    public static CarDetailBean carDetailBean;

    public static String getCategoryName() {
        return categoryName;
    }

    public static void setCategoryName(String categoryName) {
        PriceFragment.categoryName = categoryName;
    }

    public static String categoryName;

    public ArrayList<DownPaymentBean> getDownPaymentBeanList() {
        return downPaymentBeanList;
    }

    public void setDownPaymentBeanList(ArrayList<DownPaymentBean> downPaymentBeanList) {
        this.downPaymentBeanList = downPaymentBeanList;
    }

    public ArrayList<RepaymentReriodBean> getRePaymentPeriodBeanList() {
        return rePaymentPeriodBeanList;
    }

    public void setRePaymentPeriodBeanList(ArrayList<RepaymentReriodBean> rePaymentPeriodBeanList) {
        this.rePaymentPeriodBeanList = rePaymentPeriodBeanList;
    }

    public static ArrayList<DownPaymentBean> downPaymentBeanList = new ArrayList<>();
    public static ArrayList<RepaymentReriodBean> rePaymentPeriodBeanList = new ArrayList<>();

    MyAdapter myAdapter;

    @AfterViews
    public void initView() {
        headTitleText.setText("购车计算器");

        myAdapter = new MyAdapter(getChildFragmentManager(), titles);
        pager.setAdapter(myAdapter);
        tabs.setViewPager(pager);

       // myAdapter.update(carDetailBean,downPaymentBeanList,rePaymentPeriodBeanList);

    }

    @Override
    public void onStart() {
        super.onStart();
        LogUtils.println("------PriceFragment onStart-------");
    }

    @Override
    public void onResume() {
        super.onResume();
        LogUtils.println("------PriceFragment onResume-------");
    }

    @Override
    public void onPause() {
        super.onPause();
        LogUtils.println("------PriceFragment onPause-------");
    }

    @Override
    public void onStop() {
        super.onStop();
        LogUtils.println("------PriceFragment onStop-------");
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        LogUtils.println("------PriceFragment onDestroyView-------");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        LogUtils.println("------PriceFragment onDestroy-------");
        allPriceFragment.onDetach();
        loanPriceFragment.onDetach();
        allPriceFragment = null;
        loanPriceFragment = null;
    }

    public void refreshUI() {
        if (allPriceFragment != null){
            allPriceFragment.initUIData();
        }
        if (loanPriceFragment != null){
            loanPriceFragment.initUIData();
        }
    }

    class MyAdapter extends FragmentPagerAdapter {
        String[] _titles;
        FragmentManager fm;

        public MyAdapter(FragmentManager fm, String[] titles) {
            super(fm);
            this.fm = fm;
            _titles = titles;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return _titles[position];
        }

        @Override
        public int getCount() {
            return _titles.length;
        }

        @Override
        public Fragment getItem(int position) {
            switch (position) {
                case 0:
                    if (allPriceFragment == null) {
                        allPriceFragment = new AllPriceFragment_();//AllPriceFragment_.builder().build();
                        //allPriceFragment.setCarDetailBean(carDetailBean);
                        allPriceFragment.setDownPaymentBeanList(downPaymentBeanList);
                        allPriceFragment.setRePaymentPeriodBeanList(rePaymentPeriodBeanList);
                    }
                    return allPriceFragment;
                case 1:
                    if (loanPriceFragment == null) {
                        loanPriceFragment = new LoanPriceFragment_();//LoanPriceFragment_.builder().build();
                        //loanPriceFragment.setCarDetailBean(carDetailBean);
                        loanPriceFragment.setDownPaymentBeanList(downPaymentBeanList);
                        loanPriceFragment.setRePaymentPeriodBeanList(rePaymentPeriodBeanList);
                    }
                    return loanPriceFragment;
                default:
                    if (allPriceFragment == null) {
                        allPriceFragment = new AllPriceFragment_();//AllPriceFragment_.builder().build();
                        //allPriceFragment.setCarDetailBean(carDetailBean);
                        allPriceFragment.setDownPaymentBeanList(downPaymentBeanList);
                        allPriceFragment.setRePaymentPeriodBeanList(rePaymentPeriodBeanList);
                    }
                    return allPriceFragment;
            }
        }


//        @Override
//        public Object instantiateItem(ViewGroup container, int position) {
//            switch (position) {
//                case 0:
//                    AllPriceFragment f = (AllPriceFragment) super.instantiateItem(container, position);
//                    if (allPriceFragment == null) {
//                        allPriceFragment = new AllPriceFragment_();//AllPriceFragment_.builder().build();
//                        allPriceFragment.setCarDetailBean(carDetailBean);
//                        allPriceFragment.setDownPaymentBeanList(downPaymentBeanList);
//                        allPriceFragment.setRePaymentPeriodBeanList(rePaymentPeriodBeanList);
//                    }
//                    return allPriceFragment;
//                case 1:
//
//
//            }
//
//
//            String title = mList.get(position);
//            f.setTitle(title);
//            return f;
//        }
//
//        @Override
//        public int getItemPosition(Object object) {
//            return PagerAdapter.POSITION_NONE;
//        }
    }


}
